import turtle
import math

# 初始位置设置，人在正方形中心，鳄鱼在四个顶点
positionHuman = (0.00, 0.00)
positionCrocodile1 = (-100.00, 100.00)  # 左上顶点
positionCrocodile2 = (100.00, 100.00)   # 右上顶点
positionCrocodile3 = (100.00, -100.00)  # 右下顶点
positionCrocodile4 = (-100.00, -100.00) # 左下顶点

# 速度设置
human_speed = 10
crocodile_speed1 = 20  # 鳄鱼A，左上顶点
crocodile_speed2 = 20  # 鳄鱼B，右上顶点
crocodile_speed3 = 15  # 鳄鱼C，右下顶点
crocodile_speed4 = 20  # 鳄鱼D，左下顶点

# 时间步长
time_step = 0.2

turtle.pensize(3)
total_time = 0

# 设置动画更新频率和延迟时间，减慢显示速度
turtle.tracer(1, 0.1)  # 更新速度更慢，延迟

# 打开文件用于记录坐标
with open('movement_trajectory.txt', 'w') as file:
    while True:
        # 记录当前时刻的坐标到文件
        file.write(f"Time: {total_time:.1f}, Human: {positionHuman}, "
                   f"Croc1: {positionCrocodile1}, Croc2: {positionCrocodile2}, "
                   f"Croc3: {positionCrocodile3}, Croc4: {positionCrocodile4}\n")

        # 绘制人的轨迹
        turtle.color("black")
        turtle.penup()
        turtle.goto(positionHuman)
        turtle.pendown()
        turtle.dot(5, "black")  # 绘制人当前位置的点
        turtle.goto(positionHuman)  # 绘制人的轨迹为流动线

        # 计算鳄鱼到人的距离
        def calculate_distance(p1, p2):
            return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

        # 尝试更多方向的移动（包括斜向）
        best_direction = None
        best_time = -float('inf')
        directions = [
            (0, 1), (0, -1), (1, 0), (-1, 0),  # 上、下、左、右
            (1, 1), (-1, 1), (1, -1), (-1, -1)  # 斜向
        ]
        for dx, dy in directions:
            new_position = (positionHuman[0] + human_speed * time_step * dx,
                            positionHuman[1] + human_speed * time_step * dy)
            # 计算四条鳄鱼到达新位置所需的时间
            time1 = calculate_distance(new_position, positionCrocodile1) / crocodile_speed1
            time2 = calculate_distance(new_position, positionCrocodile2) / crocodile_speed2
            time3 = calculate_distance(new_position, positionCrocodile3) / crocodile_speed3
            time4 = calculate_distance(new_position, positionCrocodile4) / crocodile_speed4
            min_time = min(time1, time2, time3, time4)
            if min_time > best_time:
                best_time = min_time
                best_direction = (dx, dy)

        # 人朝着最优方向移动
        positionHuman = (positionHuman[0] + human_speed * time_step * best_direction[0],
                         positionHuman[1] + human_speed * time_step * best_direction[1])
        turtle.goto(positionHuman)

        # 鳄鱼A的移动
        turtle.color("green")
        turtle.penup()
        turtle.goto(positionCrocodile1)
        turtle.dot(5, "green")
        turtle.pendown()
        turtle.setheading(turtle.towards(positionHuman))
        turtle.fd(crocodile_speed1 * time_step)
        positionCrocodile1 = turtle.position()

        # 鳄鱼B的移动
        turtle.color("red")
        turtle.penup()
        turtle.goto(positionCrocodile2)
        turtle.dot(5, "red")
        turtle.pendown()
        turtle.setheading(turtle.towards(positionHuman))
        turtle.fd(crocodile_speed2 * time_step)
        positionCrocodile2 = turtle.position()

        # 鳄鱼C的移动
        turtle.color("blue")
        turtle.penup()
        turtle.goto(positionCrocodile3)
        turtle.dot(5, "blue")
        turtle.pendown()
        turtle.setheading(turtle.towards(positionHuman))
        turtle.fd(crocodile_speed3 * time_step)
        positionCrocodile3 = turtle.position()

        # 鳄鱼D的移动
        turtle.color("purple")
        turtle.penup()
        turtle.goto(positionCrocodile4)
        turtle.dot(5, "purple")
        turtle.pendown()
        turtle.setheading(turtle.towards(positionHuman))
        turtle.fd(crocodile_speed4 * time_step)
        positionCrocodile4 = turtle.position()

        # 检查是否有鳄鱼追上人
        if any(calculate_distance(positionHuman, croc_pos) <= 1 for croc_pos in
               [positionCrocodile1, positionCrocodile2, positionCrocodile3, positionCrocodile4]):
            break

        total_time += time_step

# 完成绘制后更新显示
turtle.update()

print(f"这个人最多还能活 {total_time:.2f} 秒")
turtle.done()
